﻿namespace Nello.Web
{
    using Nello;
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class JavaScript
    {
        public static readonly string ALERT_TEMPLATE = "alert('{0}');";
        public static readonly string REDIRECT_TEMPLATE = "window.location='{0}';";
        public static readonly string SCRIPT_TEMPLATE = "<script language=javascript>{0}</script>";

        public static void AddOnClick(WebControl ctl, string pscript)
        {
            ctl.Attributes.Add("onclick", pscript);
        }

        public static void AddToFormBegin(Page page, string pScript)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), Nello.Util.NewGuid(), string.Format(SCRIPT_TEMPLATE, pScript));
        }

        public static void AddToFormEnd(Page page, string pScript)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Nello.Util.NewGuid(), string.Format(SCRIPT_TEMPLATE, pScript));
        }

        public static void Alert(string msg)
        {
            Run(string.Format(ALERT_TEMPLATE, msg));
        }

        public static void Alert(Page page, string msg)
        {
            Run(page, string.Format(ALERT_TEMPLATE, msg));
        }

        public static void AlertAndRedirect(string msg, string url)
        {
            msg = string.Format(ALERT_TEMPLATE, msg);
            url = string.Format(REDIRECT_TEMPLATE, url);
            Run(msg + url);
        }

        public static void AlertError(Exception ex)
        {
            Alert("错误源：" + DoReplace(ex.Source) + @"\n错误描述：" + DoReplace(ex.Message));
        }

        public static void AlertError(string msg)
        {
            Alert(DoReplace(msg));
        }

        public static void CloseWindow()
        {
            Run("window.opener=null;window.close();");
        }

        public static string DoReplace(string msg)
        {
            msg = msg.Replace(@"\", "[");
            msg = msg.Replace("'", "‘");
            return msg;
        }

        public static string GetCookie(string pName)
        {
            return GetCookie(pName, true);
        }

        public static string GetCookie(string pName, bool isEnc)
        {
            string s = "";
            if (HttpContext.Current.Request.Cookies[pName] != null)
            {
                s = HttpContext.Current.Request.Cookies[pName].Value;
                if (isEnc)
                {
                    s = DESUtil.Decrypt(s);
                }
            }
            return s;
        }

        public static void NoCache()
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

        public static void Open(string pURL)
        {
            Open(pURL, 300, 500, 150, 250);
        }

        public static void Open(Page page, string pURL)
        {
            Open(page, pURL, 300, 500, 150, 250);
        }

        public static void Open(string pURL, int height, int width, int top, int left)
        {
            string mode = string.Concat(new object[] { "height=", height, ",width=", width, ",top=", top, ",left=", left, ",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no" });
            Run("window.open('" + pURL + "','" + Nello.Util.NewGuid() + "','" + mode + "');");
        }

        public static void Open(Page page, string pURL, int height, int width, int top, int left)
        {
            string mode = string.Concat(new object[] { "height=", height, ",width=", width, ",top=", top, ",left=", left, ",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no" });
            Run(page, "window.open('" + pURL + "','" + Nello.Util.NewGuid() + "','" + mode + "');");
        }

        public static void OpenWindow(Exception ex)
        {
            Run("win=window.open('','错误','height=300,width=500,top=150,left=250,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');");
            Run("win.document.write('错误源：');");
            Run("win.document.write('" + DoReplace(ex.Source) + "');");
            Run("win.document.write('<br>');");
            Run("win.document.write('错误描述：');");
            Run("win.document.write('" + DoReplace(ex.Message) + "');");
            Run("win.document.close();");
        }

        public static void OpenWindow(string msg)
        {
            Run("win=window.open('','提示','height=300,width=500,top=150,left=250,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');");
            Run("win.document.write('" + msg + "');");
            Run("win.document.close();");
        }

        public static void Redirect(Page page, string url)
        {
            Run(page, string.Format(REDIRECT_TEMPLATE, url));
        }

        public static void Run(string pScript)
        {
            HttpContext.Current.Response.Write(string.Format(SCRIPT_TEMPLATE, pScript));
        }

        public static void Run(Page page, string pScript)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), string.Format(SCRIPT_TEMPLATE, pScript));
        }

        public static void SetCookie(string pName, string pValue)
        {
            SetCookie(pName, pValue, (double) 365.0);
        }

        public static void SetCookie(string pName, string pValue, bool isEnc)
        {
            SetCookie(pName, pValue, 365.0, isEnc);
        }

        public static void SetCookie(string pName, string pValue, double days)
        {
            SetCookie(pName, pValue, days, true);
        }

        public static void SetCookie(string pName, string pValue, double days, bool isEnc)
        {
            if (isEnc)
            {
                pValue = DESUtil.Encrypt(pValue);
            }
            HttpContext.Current.Response.Cookies[pName].Value = pValue;
            HttpContext.Current.Response.Cookies[pName].Expires = DateTime.Now.AddDays(days);
        }

        public static void SetFocus(Page page, Control ctl)
        {
            Run(page, "document.all." + ctl.ClientID + ".focus();");
        }

        public static void SetHTMLControlEnabled(Page page, string ControlID, bool bEnabled)
        {
            SetHTMLControlEnabled(page, "Form1", ControlID, bEnabled);
        }

        public static void SetHTMLControlEnabled(Page page, string FormID, string ControlID, bool bEnabled)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), string.Format(SCRIPT_TEMPLATE, string.Concat(new object[] { "window.", FormID, ".", ControlID, ".disabled=", !bEnabled, ";" })));
        }
    }
}

